package fy.mdm.dao;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.IMsgObject.MOType;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;
import fy.config.FyConfig;
import fy.mdm.bean.CodingChangesBean;
import fy.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CodingChangesDao
{
    private CodingChangesBean CodingChangesBean;
    private static Log log = LogFactory.getLog(CodingChangesDao.class);
    private static final String LOOKUP_XIALA_XINGJI = "SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.REF_ENUMID=?";

    public boolean ProductCodingChanges()
            throws Exception
    {
        boolean success = false;
        String produktklassifizierungname = null;
        String statename = null;
        try
        {
            for (int i = 0; i < this.CodingChangesBean.getOldProductCode().size(); i++)
            {
                if (((String)this.CodingChangesBean.getProduktklassifizierung().get(i)).equals("0")) {
                    produktklassifizierungname = "MDM_PROIDUNCT_OEM_F";
                }
                if (((String)this.CodingChangesBean.getProduktklassifizierung().get(i)).equals("1")) {
                    produktklassifizierungname = "MDM_PROIDUNCT_OTHER_F";
                }
                if (((String)this.CodingChangesBean.getProduktklassifizierung().get(i)).equals("2")) {
                    produktklassifizierungname = "MDM_HALF_PINT_OEM_T";
                }
                if (((String)this.CodingChangesBean.getProduktklassifizierung().get(i)).equals("3")) {
                    produktklassifizierungname = "MDM_HALF_PINT_OTHER_T";
                }
                if (((String)this.CodingChangesBean.getState().get(i)).equals("0")) {
                    statename = "P000007.09";
                }
                if (((String)this.CodingChangesBean.getState().get(i)).equals("1")) {
                    statename = "变更";
                }
                IServiceRequester Requester = BaseServiceRequester.getInstance();
                List<GroupRecord> grs1 = new ArrayList();

                IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
                String SourceSysID = "06004";
                String ServiceID = "06001000000044";

                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String format = simpleDateFormat.format(date);

                reqMo.setSourceSysID(SourceSysID);
                reqMo.setSerialNO(format.substring(0, 8) + SourceSysID +
                        String.format("%06d", new Object[] { Integer.valueOf(++MaterialRequestNoTimerTask.requestNo) }));
                reqMo.setServiceID(ServiceID);
                reqMo.setServiceDateTime(format);
                GroupRecord gr1 = new GroupRecord();
                gr1.setName("ROW");
                gr1.setFieldValue("oldProductCodeList", (String)this.CodingChangesBean.getOldProductCode().get(i));
                gr1.setFieldValue("newProductCode", (String)this.CodingChangesBean.getNewProductCode().get(i));
                gr1.setFieldValue("newProductName", (String)this.CodingChangesBean.getNewProductName().get(i));
                gr1.setFieldValue("newModelType", (String)this.CodingChangesBean.getNewModelType().get(i));
                gr1.setFieldValue("newCustomerPartNumber", (String)this.CodingChangesBean.getNewCustomerPartNumber().get(i));
                gr1.setFieldValue("state", statename);
                gr1.setFieldValue("produktklassifizierung", produktklassifizierungname);
                grs1.add(gr1);
                reqMo.setReqGroupRecord(grs1);
                log.info("reqMo:" + reqMo);
                IMsgObject respMo = null;
                respMo = Requester.execute(reqMo);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    public String getUnitCode(String code, String temp)
    {
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try
        {
            conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
            pst = conn.prepareStatement("SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.REF_ENUMID=?");
            pst.setString(1, code);
            pst.setString(2, temp);
            rs = pst.executeQuery();
            if (rs.next()) {
                code = rs.getString(1);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.close(rs, pst);
        }
        return code;
    }

    public CodingChangesBean getCodingChangesBean()
    {
        return this.CodingChangesBean;
    }

    public void setCodingChangesBean(CodingChangesBean codingChangesBean)
    {
        this.CodingChangesBean = codingChangesBean;
    }
}
